cmake_minimum_required(VERSION 3.10)

project(lv_phone)

if(${INTERPHONE_DEMO} STREQUAL "FALSE")
  set(FONT_SRC ttf)
  #set(FONT_SRC bin)
else()
  set(FONT_SRC cfile)
endif()

set(lv_phone_sources
  framework/desktop/desktop_dial.c
  framework/desktop/desktop_goto.c
  framework/desktop/desktop_main.c
  framework/desktop/desktop_switch_off.c
  framework/desktop/desktop_menu.c
  framework/desktop/desktop_status_bar.c
  framework/desktop/desktop_key_guard.c
  framework/ime/ime_main.c
  framework/common/ui_app_mng.c
  framework/common/ui_common.c
  framework/common/utility.c
  framework/common/ui_log.c
  framework/common/nav.c
  framework/common/ui_msg_api.c
  framework/common/lv_theme_simple.c
  framework/nvm/ui_nvm_api.c
  framework/message/phone_msg_handle.c
  apps/alarm/alarm_proc.c
  apps/alarm/alarm_ui.c
  apps/alarm/alarm_main.c
  apps/alarm/alarm_api.c
  apps/contact/contact_delete.c
  apps/contact/contact_details.c
  apps/contact/contact_ram.c
  apps/contact/contact_main.c
  apps/contact/contact_speed_dial.c
  apps/contact/contact_search.c
  apps/contact/contact_copy.c
  apps/contact/contact_add.c
  apps/contact/contact_number_scrn.c
  apps/contact/contact_move.c
  apps/contact/contact_settings.c
  apps/contact/contact_select.c
  apps/call/call_proc.c
  apps/call/call_ui.c
  apps/call/call_main.c
  apps/call/call_api.c
  apps/settings/setting_main.c
  apps/settings/setting_time.c
  apps/settings/setting_phone.c
  apps/settings/setting_security.c
  apps/settings/setting_display.c
  apps/settings/setting_tone.c
  apps/settings/setting_dualsim.c
  apps/settings/setting_restore.c
  apps/settings/setting_profile.c
  apps/settings/setting_call.c
  apps/settings/setting_volte_switch.c
  apps/settings/setting_bluetooth_ui.c
  apps/settings/setting_bluetooth_msg.c
  apps/settings/setting_wlan.c
  apps/calllog/call_log_proc.c
  apps/calllog/call_log_api.c
  apps/calllog/call_log_ui.c
  apps/calllog/call_log_main.c
  apps/sms/sms_create.c
  apps/sms/sms_distribution.c
  apps/sms/sms_setting.c
  apps/sms/sms_scrn.c
  apps/sms/sms_ussd.c
  apps/sms/sms_api.c
  apps/sms/sms_sent.c
  apps/sms/sms_delete.c
  apps/sms/sms_common.c
  apps/sms/sms_main.c
  apps/sms/sms_receive.c
  apps/sms/sms_draft.c
  apps/calculator/calc_main.c
  apps/calendar/cale_main.c
  apps/calendar/cale_ui.c
  apps/calendar/cale_api.c
  apps/calendar/cale_proc.c
  apps/audio_player/audio_main.c
  modem/mmi_modem_adaptor_call.c
  modem/mmi_modem_adaptor_main.c
  modem/mmi_modem_adaptor_nw.c
  modem/mmi_modem_adaptor_sim.c
  modem/mmi_modem_adaptor_sms.c
  modem/mmi_modem_adaptor_ss.c
  modem/modem_adp.c
  modem/mmi_modem_adaptor_sms_file.c
  modem/mmi_modem_adaptor_ps.c
  resource/ui_ringtone.c
  resource/audio_msg.c
  resource/audio_power_on.c
#below is for pc test
  test/dummy_modem_adp.c
  test/dummy_ril.c
  test/img_phone.c
  test/phone_test_main.c
  framework/common/ui_common_ut.c
#below is for interphone
  test/img_radiobg.c
  apps/interphone/interphone_main.c
#below is for 128*64 demo
  apps/demo128x64/demo_main.c
)

if(${INTERPHONE_DEMO} STREQUAL "FALSE")
  set(lv_phone_font_sources
    resource/src/font/lv_font_droidSansFallbackFull_20.c
    resource/src/font/lv_font_droidSansFallbackFull_30_compressed.c
    #resource/src/font/lv_font_montserrat_12.c
    #resource/src/font/lv_font_montserrat_12_subpx.c
    #resource/src/font/lv_font_montserrat_14.c
    #resource/src/font/lv_font_montserrat_20.c
    #resource/src/font/lv_font_montserrat_24.c
    #resource/src/font/lv_font_montserrat_28_compressed.c
    #resource/src/font/lv_font_montserrat_40.c
  )
else()
  set(lv_phone_font_sources
    resource/src/font/lv_font_droidSansFallbackFull_16.c
  )
endif()

if(${LV_I18N_ENABLE} STREQUAL "TRUE")
  set(lv_phone_multi_lang_sources
    framework/language/lv_i18n/lv_i18n.c
    framework/language/lv_i18n/lv_i18n_lang.c
  )
else()
  set(lv_phone_multi_lang_sources
    framework/language/lang_en.c
    framework/language/lang_ch.c
    framework/language/lv_lang.c
  )
endif()

set(lv_phone_raw_alpha_resources
  png/add_name.png
  png/alarm.png
  png/alarm_ring.png
  png/call_calling.png
  png/call_end.png
  png/call_hold.png
  png/call_incoming.png
  png/call_missed_l.png
  png/call_missed.png
  png/call_outgoing.png
  png/call_phone.png
  png/call_timer.png
  png/check.png
  png/copy.png
  png/counter.png
  png/dialed_call.png
  png/display.png
  png/distribution.png
  png/draft.png
  png/d_sim.png
  png/flight_mode.png
  png/inbox.png
  png/menu_alarm.png
  png/menu_calculator.png
  png/menu_calendar.png
  png/menu_calllog.png
  png/menu_contact.png
  png/menu_setting.png
  png/menu_sms.png
  png/missed_call.png
  png/no_sim1_20.png
  png/no_sim2_20.png
  png/no_sim_card.png
  png/off_en.png
  png/off.png
  png/on_en.png
  png/on.png
  png/phone.png
  png/profile.png
  png/progressbar.png
  png/read_sms.png
  png/received_call.png
  png/reset.png
  png/screened.png
  png/search.png
  png/security.png
  png/sent.png
  png/sent_sms.png
  png/service.png
  png/signal0.png
  png/signal1.png
  png/signal2.png
  png/signal3.png
  png/signal4.png
  png/signal5.png
  png/sim1_20.png
  png/sim2_20.png
  png/speaker_louder.png
  png/speed_dial.png
  png/time.png
  png/tone.png
  png/transfer.png
  png/trash.png
  png/uncheck.png
  png/unread_sms_l.png
  png/unread_sms.png
  png/volume_muted.png
  png/wallpaper1.png
  png/wallpaper1_s.png
  png/interphone_distribution.png
  png/interphone_check.png
  png/interphone_uncheck.png
  png/interphone_add_name.png
  png/refresh_1.png
  png/refresh_2.png
  png/eye_no_black.png
  png/eye_black.png
  png/lock.png
  png/wifi_full.png
  png/wifi_high.png
  png/wifi_low.png
  png/wifi_mid.png
)

# generate lv_i18n source files
if(${LV_I18N_ENABLE} STREQUAL "TRUE")
  set(LV_I18N_SOURCE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/framework/language/lv_i18n")
  set(LV_I18N_TOOL "${MISC_TOOLS_PATH}/lv_i18n")
  execute_process(
  COMMAND ${LV_I18N_TOOL} asrcompile -t ${LV_I18N_SOURCE_PATH}/*.yml -o ${LV_I18N_SOURCE_PATH}
)
endif()

# if run time load from font binary files
set(lv_phone_font_bin_resources
  if(${INTERPHONE_DEMO} STREQUAL "TRUE")
    font/lv_font_droidSansFallbackFull_16.bin
  else()
  font/lv_font_droidSansFallbackFull_20.bin
  font/lv_font_droidSansFallbackFull_30_compressed.bin
  endif()
)

# if run time load from font ttf
set(lv_phone_font_ttf_resources
  ttf/YH2312F.ttf
)

# Add the static library
if(${FONT_SRC} STREQUAL "cfile")
  add_library(lv_phone STATIC  ${lv_phone_sources} ${lv_phone_font_sources} ${lv_phone_multi_lang_sources})
else()
  add_library(lv_phone STATIC  ${lv_phone_sources} ${lv_phone_multi_lang_sources})
endif()

target_include_directories(lv_phone PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${PRODUCT_CONFIG_DIR} ${SDL2_INCLUDE_DIRS} ${LVGL_DIR} include framework/common resource)
if("x${CMAKE_C_COMPILER_ID}" STREQUAL "xARMCC")
  target_compile_options(lv_phone PRIVATE --signed_chars)
else()
  target_compile_options(lv_phone PRIVATE -fsigned-char)
endif()
target_link_libraries(lv_phone PRIVATE lvgl)

set(RESOURCE "resource")
set(RESOURCE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE}")
set(RESOURCE_C_PATH "${CMAKE_CURRENT_SOURCE_DIR}/resource_c")
set(IMG_CONV_CORE_PHP "${CMAKE_CURRENT_SOURCE_DIR}/../../tools/lv_utils/img_conv_core.php")

set(resource_target "lv_phone_generate_resource_romfs")
set(resource_depends_srcs "")
set(resource_output "${CMAKE_CURRENT_BINARY_DIR}/../resource.bin")

function(LIST_ROMFS_DEPENDS_FUN input_list prefix)
  list(TRANSFORM input_list PREPEND ${prefix} OUTPUT_VARIABLE output_list)
  set(resource_depends_srcs ${resource_depends_srcs} ${output_list} PARENT_SCOPE)
endfunction()

LIST_ROMFS_DEPENDS_FUN("${lv_phone_raw_resources}" "${RESOURCE}/")
LIST_ROMFS_DEPENDS_FUN("${lv_phone_raw_alpha_resources}" "${RESOURCE}/")
LIST_ROMFS_DEPENDS_FUN("${lv_phone_true_color_resources}" "${RESOURCE}/")

set(X_DIR1 "FONT_DIR1")
set(X_DIR2 "FONT_DIR2")
if(${FONT_SRC} STREQUAL "ttf")
  set(X_DIR1 "font")
  add_definitions(-DUSE_FONT_TTF)
elseif(${FONT_SRC} STREQUAL "bin")
  set(X_DIR1 "ttf")
  add_definitions(-DUSE_FONT_BIN)
else()
  set(X_DIR1 "font")
  set(X_DIR2 "ttf")
endif()

set(GENROMFS "${MISC_TOOLS_PATH}/genromfs")
add_custom_command(
  OUTPUT ${resource_output}
  COMMAND_EXPAND_LISTS
  #COMMAND echo "${GENROMFS} -V \"resource\" -f ${resource_output} -d ${RESOURCE_PATH} -x *.jpg  -x font"
  COMMAND ${GENROMFS}
  ARGS -V "resource" -f ${resource_output} -d ${RESOURCE_PATH} -x *.jpg -x *.c -x *.h -x ${X_DIR1} -x ${X_DIR2}
  DEPENDS ${resource_depends_srcs}
  COMMENT "create ${resource_output}"
)

if(${USE_INTERNAL_ROMFS} STREQUAL "TRUE")

  set(IMG2C python ${CMAKE_CURRENT_SOURCE_DIR}/img2c.py)
  set(python_output "${CMAKE_CURRENT_SOURCE_DIR}/resource.h")
  add_custom_command(
    OUTPUT ${python_output}
    COMMAND_EXPAND_LISTS
    COMMAND ${IMG2C}
    ARGS  ${resource_output} ${python_output}
    DEPENDS ${resource_output}
    COMMENT "create ${python_output}"
  )

  add_custom_target(${resource_target}
    DEPENDS ${python_output}
  )

else()

  add_custom_target(${resource_target}
    DEPENDS ${resource_output}
  )

endif()
add_dependencies(lv_phone ${resource_target})
